package org.marsson.bNav;

import java.io.Serializable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;


class PlayingField extends JPanel implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Point lastclicked = new Point();
	public static final int gridSize = 16;
	public static final int SPLASH = 0,
							SEAWOLF = 2,
							BATTLESHIP = 3,
							IDLE = 0,
							SHIP_PLACEMENT = 1,
							POINT_SELECTION = 2;

	
	transient public int 	mode = IDLE;
	transient public Ships 	enumShip;
	transient public ShipButton 
	
	seawolfB, 
	battleshipB; 
	
	private boolean shipReady = false;
	static Image target, logo, fire, splash;
	transient private JPanel fields, buttonPanel, statusPanel;
	transient public GridArea enemyOcean;
	private RightField myOcean;
	transient private JTextArea status;
	
	
	

	public PlayingField(String gameTitle)
	{
		setLayout(new BorderLayout());

		splash = (new ImageIcon("src/main/resources/splash.gif")).getImage();
		fire = (new ImageIcon("src/main/resources/fire.gif")).getImage();
		target = (new ImageIcon("src/main/resources/target.gif")).getImage();
		logo = (new ImageIcon("src/main/resources/logo.jpg")).getImage();

		fields = new JPanel();
		enemyOcean = new LeftField(this,gridSize);
		myOcean = new RightField(this, gridSize);

		fields.setLayout(new FlowLayout());
		fields.add(enemyOcean);
		fields.add(new LogoPanel());
		fields.add(myOcean);
		fields.setBorder(new TitledBorder(gameTitle));

		status = new JTextArea("Bem Vindo a Batalha Naval/n", 4, 0);
		(statusPanel = new JPanel()).setLayout(new BorderLayout());
		statusPanel.setBorder(new TitledBorder("Status do Jogo"));
		statusPanel.add(new JScrollPane(status, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);

		add(fields, BorderLayout.CENTER);
		//myOcean.showGrid();
		ButtonHandler itemHandler = new ButtonHandler();

		
		(seawolfB = new ShipButton("Seawolf", Ships.SEAWOLF.imageH())).addActionListener(itemHandler);
		(battleshipB = new ShipButton("Battleship",Ships.BATTLESHIP.imageH())).addActionListener(itemHandler);
		

		JPanel shipsTop = new JPanel(new GridLayout(1,2));
		
		shipsTop.add(battleshipB);

		JPanel shipsBottom = new JPanel(new GridLayout(1,3));
		shipsBottom.add(seawolfB);
		

		JPanel ships = new JPanel(new GridLayout(2,1));

		ships.add(shipsTop);
		ships.add(shipsBottom);

		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		buttonPanel.setBorder(new TitledBorder("Select A Ship To Place on the Home Field"));
		buttonPanel.add(ships, BorderLayout.CENTER);

		add(statusPanel, BorderLayout.SOUTH);
	}

	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed( ActionEvent e )
		{
			
			if (e.getSource()==battleshipB) {
				
				enumShip = Ships.BATTLESHIP;
			
				shipReady = true;
				
			}
			
			if (e.getSource()==seawolfB) {
			
				enumShip = Ships.SEAWOLF;
	
				shipReady = true;
				
			}
		
		}
	}

	public int getMode()				{	return mode;			}
    public RightField getMyOcean(){
    	return myOcean;
    }
	
    
    public void setResult(Point coordinates, boolean acertou)
	{
    	
		if (acertou)
		{
			enemyOcean.setArea(coordinates, new Pedaco (true,false,Ships.FIRE,true));
		}
		else{
			enemyOcean.setArea(coordinates, new Pedaco (true,false,Ships.SPLASH,true));
		}
		//enemyOcean.setArea(coordinates, result + temp);
		enemyOcean.repaint();
	}

	
	public Point getPoint()
	{
		Point thePoint = null;
		mode = POINT_SELECTION;
		while (thePoint==null)
		{
			thePoint = enemyOcean.getSelected();
			try
			{
				Thread.sleep(10);
			}
			catch(InterruptedException ie)	{ie.printStackTrace();}
		}
		

		mode = IDLE;
		return thePoint;
	}
//IMPORTANTEEEEE
	public Pedaco getHit(Point coordinates)
	{
		Pedaco theArea = myOcean.getArea(coordinates);
		theArea.setHit(true);
		myOcean.setArea(coordinates, theArea);
		myOcean.repaint();
		return theArea;
	}

	private void setThisShip(){
		lastclicked= null;
		while(lastclicked==null ||!myOcean.validPlacement()){
			lastclicked=null;
			try	{	Thread.sleep(100);	}
			catch(InterruptedException ie)	{ie.printStackTrace();}	
			
		}
		
		if (myOcean.vertical==true){
			myOcean.area[lastclicked.x][lastclicked.y]=new Pedaco(true,true,enumShip,false);
			for(int i=1;i<enumShip.size;i++){
				myOcean.area[lastclicked.x+i][lastclicked.y]=new Pedaco(true,false,enumShip,false);
			}
			//myOcean.showGrid();
		}
		else{
			myOcean.area[lastclicked.x][lastclicked.y]=new Pedaco(false,true,enumShip,false);
			for(int i=1;i<enumShip.size;i++){
				myOcean.area[lastclicked.x][lastclicked.y+i]=new Pedaco(false,false,enumShip,false);
			}
			//myOcean.showGrid();
		}
		
		//Codigo porco
		if(enumShip.index==3){
			battleshipB.decreaseQuantity();
		}
		else{
			seawolfB.decreaseQuantity();
		}
		
		enumShip=null;
		shipReady=false;
		validate();
		
	}
	public void placeShips()
	{
		remove(statusPanel);
		add(buttonPanel, BorderLayout.SOUTH);
		validate();
		mode = 2;
		boolean moreShips = true;
		while (moreShips)
		{
			moreShips = battleshipB.isEnabled() ||
			 seawolfB.isEnabled() ;
			
			if(shipReady==true){
				
				setThisShip();
				
			}
			
			try	{	Thread.sleep(100);	}
			catch(InterruptedException ie)	{ie.printStackTrace();}
			
			
			
		};
		
		//myOcean.showGrid();
		enumShip = null;
		remove(buttonPanel);
		add(statusPanel, BorderLayout.SOUTH);
		SwingUtilities.updateComponentTreeUI(this);
		mode = IDLE;
		
	}

	public void addMessage(String message)
	{
		status.append(message + "\n");
		status.setCaretPosition(status.getText().length());
	}
}

class LeftField extends GridArea
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int gridSize;
	public LeftField(PlayingField handle, int size)
	{
		super("Opponents Field", handle,size);
		this.gridSize = size;
	}

	public void paintComponent(Graphics g)
 {

		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		//roda o grid
		for (int x = 0; x < this.gridSize; x++)
			for (int y = 0; y < this.gridSize; y++) {
				//Caso a area nao seja vazia:
				if (area[x][y].isHit()) {
					g2.drawImage(area[x][y].getNavio().imageH.getImage(), 25*y, 25*x, this);
				}

			}
		if (cursorLocation != null)
			g2.drawImage(PlayingField.target, 25 * (int) cursorLocation.getY(),
					25 * (int) cursorLocation.getX(), this);
	}
}
		
		


class RightField extends GridArea
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public RightField(PlayingField handle, int size)
	{
		
		super("Home Field", handle, size);
	}

	public void paintComponent(Graphics g)
	{
		 
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;

		
		
		
		for (int i = 0;i<area.length;i++){
			for (int j = 0;j<area.length;j++){
				if(area[i][j].getNavio()!=Ships.SPLASH&&area[i][j].getNavio()!=Ships.FIRE &&area[i][j].isCabeca()){
					if(area[i][j].isVertical()){
						g2.drawImage(area[i][j].getNavio().imageV.getImage(), 25*j, 25*i, this);
					}
					else{
						g2.drawImage(area[i][j].getNavio().imageH.getImage(), 25*j, 25*i, this);	
					}
				}
				
				if(area[i][j].isHit()){
					if(area[i][j].getNavio()==Ships.SPLASH){
						g2.drawImage(area[i][j].getNavio().imageV.getImage(), 25*j, 25*i, this);
					}
					else{
						g2.drawImage(Ships.FIRE.imageV.getImage(), 25*j, 25*i, this);
					}
						
				}
				
			}
		}
		if (mainHandle.enumShip!=null && validPlacement())
		{
				if (!vertical){
				
				g2.fill3DRect(25*(int)cursorLocation.getY(),
		
				
						25*(int)cursorLocation.getX(), 25*mainHandle.enumShip.size, 25, false);
				
			}
			
				else{
			
			}
			if (vertical) {
				
				g2.fill3DRect(25*(int)cursorLocation.getY(),
				
						25*(int)cursorLocation.getX(), 25, 25*mainHandle.enumShip.size, false);
				
				}
		}
	
	
	
	}
	
}
			

class LogoPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Dimension getPreferredSize()		{	return new Dimension(100,270);	}
	public void paintComponent(Graphics g)	{	g.drawImage(PlayingField.logo, 0, 0, this);	}
}